package com.jason.campusinn.view;

import android.app.Activity;
import android.content.Context;
import android.content.res.AssetManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import com.yuandian.wanna.R;
import com.yuandian.wanna.utils.CommonMethodUtils;
import com.yuandian.wanna.utils.LogUtil;
import com.yuandian.wanna.wheelview.ArrayWheelAdapter;
import com.yuandian.wanna.wheelview.OnWheelChangedListener;
import com.yuandian.wanna.wheelview.WheelView;
import com.yuandian.wanna.wheelview.XmlNormalParserHandler;

import java.io.InputStream;
import java.util.Calendar;
import java.util.List;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

/**
 * Created by yuandian on 2015/5/21.
 */
public class PopupWindowBirthday extends android.widget.PopupWindow implements OnWheelChangedListener {
    private View view;
    private Activity context;
    private WheelView mViewFirst;
    private WheelView mViewSecond;
    private WheelView mViewThird;

    private String value;

    protected String[] mFirstDatas;
    protected String[] mSecondDatas = {"1月", "2月", "3月", "4月", "5月", "6月", "7月", "8月", "9月", "10月", "11月", "12月"};
    protected String[] mThirdDatas;

    public PopupWindowBirthday(Activity context, int viewLenth) {

        this.context = context;
        //初始化页面
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = inflater.inflate(R.layout.popup_choose_birthday, null);
        setContentView(view);
        mViewFirst = (WheelView) view.findViewById(R.id.first_selection);
        mViewFirst.addChangingListener(this);
        mViewSecond = (WheelView) view.findViewById(R.id.second_selection);
        mViewSecond.addChangingListener(this);
        mViewThird = (WheelView) view.findViewById(R.id.third_selection);
        mViewThird.addChangingListener(this);
        setUpData(viewLenth);

        // 设置SelectPicPopupWindow弹出窗体的宽
        this.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        this.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        // 设置SelectPicPopupWindow弹出窗体可点击
        this.setFocusable(true);
        // 设置SelectPicPopupWindow弹出窗体动画效果
        this.setAnimationStyle(R.style.popupStyle);
        // 实例化一个ColorDrawable颜色为半透明
//        ColorDrawable dw = new ColorDrawable(0xB0FFFFFF);
        // 设置SelectPicPopupWindow弹出窗体的背景
//        this.setBackgroundDrawable(dw);
        this.setOutsideTouchable(true);
        this.update();

        setOnDismissListener(new OnDismissListener() {
            @Override
            public void onDismiss() {
                showSelectedResult();
            }
        });
    }

    @Override
    public void onChanged(WheelView wheel, int oldValue, int newValue) {
        if (wheel == mViewFirst) {
            mViewSecond.setCurrentItem(0);
            updataDay();
        } else if (wheel == mViewSecond) {
            updataDay();
        }
    }

    private void updataDay() {
        int year = 1990;
        int month = 1;
        try {
            String y = mFirstDatas[mViewFirst.getCurrentItem()];
            String m = mSecondDatas[mViewSecond.getCurrentItem()];
            LogUtil.d("select time month + " + m);
            LogUtil.d("select time year + " + y);
            year = Integer.parseInt(y.substring(0,y.length()-"年".length()));
            month = Integer.parseInt(m.substring(0, m.length() - "月".length()));
        } catch (Exception e) {
            e.printStackTrace();
        }

        int day = 31;
        switch (month) {
            case 1:
            case 3:
            case 5:
            case 7:
            case 8:
            case 10:
            case 12:
                day = 31;
                break;

            case 4:
            case 6:
            case 9:
            case 11:
                day = 30;
                break;
            case 2:
                day = 28 + isRun(year);
                break;
        }
        mThirdDatas = new String[day];
        for (int i=0; i<day; i++)
        {
            mThirdDatas[i] = i+1+"日";
        }
        mViewThird.setViewAdapter(new ArrayWheelAdapter<String>(context, mThirdDatas));
        mViewThird.setCurrentItem(0);
    }

    private int isRun(int year)
    {
       return year%400 == 0 || (year %4 == 0 && year%100 != 0) ? 1:0;
    }

    private void setUpData(int viewLenth) {
        initProvinceDatas();
        mViewFirst.setViewAdapter(new ArrayWheelAdapter<String>(context, mFirstDatas));
        mViewSecond.setViewAdapter(new ArrayWheelAdapter<String>(context, mSecondDatas));
        mViewSecond.setCurrentItem(0);
        mViewFirst.setVisibleItems(viewLenth);
        mViewSecond.setVisibleItems(viewLenth);
        mViewThird.setVisibleItems(viewLenth);
        updataDay();
    }

    /**
     * 初始化控件地区信息
     */
    protected void initProvinceDatas() {
        Calendar cal = Calendar.getInstance();
        int year = cal.get(Calendar.YEAR);
        mFirstDatas = new String[100];
        for (int i = 0; i < 100; i++) {
            mFirstDatas[i] = year-i+"年";
        }
    }

    public void setValue(String value)
    {
        if(CommonMethodUtils.isEmpty(value))
            return;
        this.value = value;
        String[] data = value.split("-");
        int year = Integer.parseInt(data[0]);
        int firstYear = Integer.parseInt(mFirstDatas[0].substring(0,mFirstDatas[0].length()-"年".length()));
        if(firstYear - year > mFirstDatas.length || year - firstYear > 0)
            return;
        mViewFirst.setCurrentItem(Math.abs(year-firstYear));
        updataDay();
        int month = Integer.parseInt(data[1]);
        mViewSecond.setCurrentItem(month-1);
        int day = Integer.parseInt(data[2]);
        mViewThird.setCurrentItem(day-1);
    }



    private void showSelectedResult() {
        String year = mFirstDatas[mViewFirst.getCurrentItem()];
        String month = mSecondDatas[mViewSecond.getCurrentItem()];
        String day = mThirdDatas[mViewThird.getCurrentItem()];
        String result = year.substring(0, year.length() - "年".length()) + "-" +
                month.substring(0,month.length()-"月".length()) + "-" +
                day.substring(0,day.length()-"日".length());
        callback.setValue(result);
    }

    private Callback callback;

    public Callback getCallback() {
        return callback;
    }

    public void setCallback(Callback callback) {
        this.callback = callback;
    }

    public interface Callback {
        void setValue(String value);
    }

}
